﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace commonlib
{
    [Serializable]
    class CentralDirectoryServices : MarshalByRefObject, ICentralDirectoryServices
    {

        #region ICentralDirectoryServices Members

        List<DataServer> registeredDataServers;
        List<Client> registeredClients;
        Dictionary<string, string> items;

        public CentralDirectoryServices()
        {
            this.registeredDataServers = new List<DataServer>();
            this.registeredClients = new List<Client>();
            this.items = new Dictionary<string, string>();
        }

        /**
         * Este método cria um novo id atribui ao component que se regista
         * e adiciona-o à lista dos componentes registados e retorna o id atribuido 
         * ao novo component do systema
         **/
        public long registerComponent(SystemComponent component)
        {
            component.id = generateId();

            if (component.GetType().Equals(typeof(Client)))
                this.registeredClients.Add((Client)component);
            else
                this.registeredDataServers.Add((DataServer)component);

            Console.WriteLine("registered new :" + component.GetType() + " name :" + component.name);

            Console.WriteLine("total count clients :{0}", this.registeredClients.Count);
            Console.WriteLine("total count dataservers :{0}", this.registeredDataServers.Count);

            return component.id;
        }

        private long generateId()
        {
            byte[] buffer = Guid.NewGuid().ToByteArray();
            return BitConverter.ToInt64(buffer, 0);
        }

        public long getTxId()
        {
            Thread.Sleep(1000);
            return DateTime.Now.Ticks;
        }

        public List<Client> getRegisteredClients()
        {
            return this.registeredClients;
        }

        public List<DataServer> getRegisteredDataServers()
        {
            Console.WriteLine("requested " + this.registeredDataServers.Count);
            return this.registeredDataServers;
        }

        public DataServer getDataServer(string name)
        {
            Console.WriteLine("search for " + name + " size:" + this.registeredDataServers.Count);
            DataServer ds = null;

            /*
            foreach(DataServer dataServer in this.registeredDataServers){
                if(dataServer.name.Equals(name))
                    return dataServer;
            }*/

            return ds;
        }

        /**
         * Regista a chave e uma string contendo os nomes dos servidores
         * em que os dados estão armazenados
         **/
        public void addKey(string newKey, string servers)
        {
            this.items.Add(newKey, servers);
        }

        /**
         * Retorna uma string contendo os nomes dos servidores contendo a chave procurada
         **/
        public string getServersContainingKey(string key)
        {
            return this.items[key];
        }

        #endregion
    }
}
